package com.project.easy.common.framework.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.easy.common.db.mybatisplus.core.util.PageConvertTool;
import  com.project.easy.common.util.constants.CommonConstant;
import  com.project.easy.common.util.dto.PageResDto;
import  com.project.easy.common.util.exception.business.BusinessException;
import  com.project.easy.common.util.string.StringUtil;
import com.project.easy.common.framework.web.entity.FrameworkSysParam;
import com.project.easy.common.framework.web.entity.dto.PageSysParamReq;
import com.project.easy.common.framework.web.entity.dto.SysParamDto;
import com.project.easy.common.framework.web.entity.dto.SysParamUpdateDto;
import com.project.easy.common.framework.web.entity.dto.SysParamVerifyCodeReq;
import com.project.easy.common.framework.web.mapper.FrameworkSysParamMapper;
import com.project.easy.common.framework.web.service.IFrameworkSysParamService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 系统参数表 服务实现类
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2022-07-20
 */
@Service
public class FrameworkSysParamServiceImpl extends ServiceImpl<FrameworkSysParamMapper, FrameworkSysParam> implements IFrameworkSysParamService {
    /**
     * 新增
     *
     * @param sysParamDto 参数
     * @return 返回 true 成功 false 失败
     */
    @Override
    public Long add(SysParamDto sysParamDto) {
        Boolean aBoolean = this.verifyCode(sysParamDto.getCode(), sysParamDto.getSysCode(), null);
        if (aBoolean) {
            throw  new BusinessException("参数编码不能重复");
        }
        FrameworkSysParam frameworkSysParam = new FrameworkSysParam();
        frameworkSysParam.setCode(sysParamDto.getCode());
        frameworkSysParam.setValue(sysParamDto.getValue());
        frameworkSysParam.setSysCode(StringUtil.isBlank(sysParamDto.getSysCode())
                ? CommonConstant.DEFAULT_SYS_CODE : sysParamDto.getSysCode());

        boolean save = this.save(frameworkSysParam);
        if (save) {
            return frameworkSysParam.getId();
        }
        throw  new BusinessException("存储失败");
    }

    /**
     * 更新数据
     *
     * @param sysParamDto 参数
     * @return true 成功 false 失败
     */
    @Override
    public Boolean updateObject(SysParamUpdateDto sysParamDto) {

        FrameworkSysParam frameworkSysParam = this.getById(sysParamDto.getId());
        if (Objects.isNull(frameworkSysParam)) {
            throw  new BusinessException("ID非法");
        }
        Boolean aBoolean = verifyCode(sysParamDto.getCode(), sysParamDto.getSysCode(), sysParamDto.getId());
        if (aBoolean) {
            throw  new BusinessException("修改参数编码不能重复");
        }
        frameworkSysParam.setCode(sysParamDto.getCode());
        frameworkSysParam.setValue(sysParamDto.getValue());
        frameworkSysParam.setSysCode(StringUtil.isBlank(sysParamDto.getSysCode())
                ? CommonConstant.DEFAULT_SYS_CODE : sysParamDto.getSysCode());
        return  this.updateById(frameworkSysParam);
    }

    /**
     * 更新数据
     * @param sysCode 系统编码
     * @param code 参数
     * @return true 成功 false 失败
     */
    @Override
    public SysParamDto queryByCode(String code, String sysCode) {
        FrameworkSysParam frameworkSysParam = querByCodeAndSysCode(code, sysCode);
        if (Objects.isNull(frameworkSysParam)) {
            return null;
        }
        SysParamDto sysParamDto = new SysParamDto();
        sysParamDto.setCode(frameworkSysParam.getCode());
        sysParamDto.setValue(frameworkSysParam.getValue());
        sysParamDto.setSysCode(frameworkSysParam.getSysCode());
        return sysParamDto;
    }

    /**
     * 根据编码与系统查询
     * @param code 编码
     * @param sysCode 系统编码
     * @return true 成功 false 失败
     */
    private FrameworkSysParam querByCodeAndSysCode(String code, String sysCode) {
        if (StringUtil.isBlank(sysCode)) {
            sysCode = CommonConstant.DEFAULT_SYS_CODE;
        }
        QueryWrapper<FrameworkSysParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        queryWrapper.eq("has_del", Boolean.FALSE);
        queryWrapper.eq("sys_code", sysCode);
        return this.getOne(queryWrapper);
    }

    /**
     * 分页
     *
     * @param param 分页查询条件
     * @return 返回数据
     */
    @Override
    public PageResDto<FrameworkSysParam> pageList(PageSysParamReq param) {
        param.setSysCode(StringUtil.isBlank(param.getSysCode()) ? CommonConstant.DEFAULT_SYS_CODE : param.getSysCode());
        Page<FrameworkSysParam> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<FrameworkSysParam> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(param.getCode())) {
            queryWrapper.like("code", param.getCode());
        }
        if (StringUtil.isNotBlank(param.getSysCode())) {
            queryWrapper.eq("sys_code", param.getSysCode());
        }
        queryWrapper.eq("has_del", Boolean.FALSE);

        Page<FrameworkSysParam> result  = this.baseMapper.selectPage(page, queryWrapper);
        return PageConvertTool.convert(result);
    }
    /**
     * 验证数据
     *
     * @param sysParamVerifyCodeReq 参数
     * @return true 验证成功 false 验证失败
     */
    @Override
    public Boolean verifyCode(SysParamVerifyCodeReq sysParamVerifyCodeReq) {
        return  verifyCode(sysParamVerifyCodeReq.getCode(), sysParamVerifyCodeReq.getSysCode(), sysParamVerifyCodeReq.getId());
    }

    /**
     * 验证数据
     * @param  sysCode 系统编码
     * @param code 编码
     * @param id ID
     * @return true 验证成功 false 验证失败
     */
    private Boolean verifyCode(String code, String sysCode, Long id) {
        if (StringUtil.isBlank(code)) {
            return Boolean.FALSE;
        }
        FrameworkSysParam dto  = this.querByCodeAndSysCode(code, sysCode);
        if (Objects.nonNull(id) && Objects.nonNull(dto)) {
            if (dto.getId().compareTo(id) == 0) {
                return Boolean.FALSE;
            }
        }
        return Objects.nonNull(dto);
    }

    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean able(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("参数不能为空");
        }
        FrameworkSysParam sysParam = this.baseMapper.selectById(id);
        if (Objects.isNull(sysParam)) {
            throw  new BusinessException("参数非法!");
        }
        if (sysParam.getStatus()) {
            throw  new BusinessException("启用不能再次启用!");
        }
        sysParam.setStatus(Boolean.TRUE);
        this.baseMapper.updateById(sysParam);
        return Boolean.TRUE;
    }

    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean disAble(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("参数不能为空");
        }
        FrameworkSysParam sysParam = this.baseMapper.selectById(id);
        if (Objects.isNull(sysParam)) {
            throw  new BusinessException("参数非法!");
        }
        if (!sysParam.getStatus()) {
            throw  new BusinessException("禁用不能再次禁用!");
        }
        sysParam.setStatus(Boolean.FALSE);
        this.baseMapper.updateById(sysParam);
        return Boolean.TRUE;
    }
}
